//
//  PersonalViewModel.m
//  BigJoy
//
//  Created by Rowling on 2017/7/3.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "PersonalViewModel.h"
#import "JFImagePickerController.h"

@implementation PersonalViewModel
-(choseTakePhotoVIew *)choseView {
    if (_choseView == nil) {
        _choseView = [[choseTakePhotoVIew alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ) titleArray:@[@"拍照",@"从相册选择",@"取消"]];
        @weakify(self);
        [_choseView.subject subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            NSNumber * number = x;
            NSInteger  interger = [number integerValue];
            if (interger == 0) {
                // 拍照
                [self openCamera];
            }
            if (interger == 1) {
                //从相册选择
                [self openPhotoFile];
            }
            if (interger == 2) {
                //取消
            }
        }];
    }
    return _choseView;
}

-(RACSubject *)choseImageSubject {
    if (_choseImageSubject == nil) {
        _choseImageSubject = [RACSubject subject];
    }
    return _choseImageSubject;
}

-(void)showTheChosePhotoView {
    [XMKEYWINDOWXM addSubview:self.choseView];
    [self.choseView doAnimation];
}

#pragma -mark:上传图片或者拍照
- (void)imagePickerDidFinished:(JFImagePickerController *)picker{
    XMweakSelf;
    for (ALAsset *asset in picker.assets) {
        [[JFImageManager sharedManager] imageWithAsset:asset resultHandler:^(CGImageRef imageRef, BOOL longImage) {
            UIImage *image = [UIImage imageWithCGImage:imageRef];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.choseImageSubject sendNext:image];
            });
        }];
    }
    [self imagePickerDidCancel:picker];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self imageHandleWithpickerController:picker MdediaInfo:info];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

- (void)imageHandleWithpickerController:(UIImagePickerController *)picker MdediaInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.choseImageSubject sendNext:image];
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

- (void)imagePickerDidCancel:(JFImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
    [JFImagePickerController clear];
}

- (void)openPhotoFile
{
     NSInteger count = 1;
    if (self.picCount > 1) {
        count = self.picCount;
    }
    [JFImagePickerController setMaxCount:count];
    JFImagePickerController *picker = [[JFImagePickerController alloc] initWithRootViewController:[UIViewController new]];
    picker.pickerDelegate = self;
    UIViewController * VC = [XMSuperHelper XMControllerOfView:self.bindView];
    [VC.navigationController presentViewController:picker animated:YES completion:nil];
}

- (void)openCamera
{
    UIImagePickerController *vc = [UIImagePickerController new];
    vc.sourceType = UIImagePickerControllerSourceTypeCamera;//sourcetype有三种分别是camera，photoLibrary和photoAlbum
    vc.delegate = self;
    UIViewController * VC = [XMSuperHelper XMControllerOfView:self.bindView];
    [VC.navigationController presentViewController:vc animated:YES completion:nil];
}

@end
